package ru.zombator.taskman.client.actions;

import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.swt.widgets.Shell;

import ru.zombator.taskman.client.ui.ClientWindow;
import ru.zombator.taskman.client.ui.dialogs.JoinProjectDialog;
import ru.zombator.taskman.core.dbclient.impl.TaskProject;
import ru.zombator.taskman.core.domain.ITaskProject;
import ru.zombator.taskman.core.query.payload.IPayload;
import ru.zombator.taskman.core.query.payload.JoinProjectRequest;
import ru.zombator.taskman.core.query.payload.ProjectJoinedResponse;

public class JoinProjectAction extends ADialogAction {
    public JoinProjectAction(ClientWindow mainWnd) {
        super(mainWnd);
    }

    @Override
    public String getText() {
        return "Присоединиться...";
    }
    
    @Override
    protected Dialog createDialog(Shell parentShell) {
        return new JoinProjectDialog(parentShell, mainWnd);
    }
    
    @Override
    protected IPayload createRequest(Dialog dlg) {
        JoinProjectDialog jpDlg = (JoinProjectDialog) dlg;
        ITaskProject mockProject = new TaskProject(jpDlg.getId(), jpDlg.getName(), null); 
        return new JoinProjectRequest(mockProject, jpDlg.getPassword());
    }
    
    @Override
    protected String processResponse(IPayload response) {
        ProjectJoinedResponse pjr = (ProjectJoinedResponse) response;
        return String.format("Присоединение к проекту %s (ID=%s) произошло успешно.",
                pjr.getProject().getName(),
                pjr.getProject().getId());
    }
    
}
